(function ($, window) {
    var baseRequest = window.baseRequest;
    var appConfig = window.appConfig;
    var commonTool = window.commonTool;
    var dataManager = window.dataManager;
    var locale = dataManager.getLocale();
    var baseUrl = appConfig.v3ServiceUrl + '/ant/confmgr';
    var v3AntConfmgr = window.v3AntConfmgr = {};

    // 处理参数，因为有好大的一部分是一样的，所以在这里处理
    function execParams(userName, shopId, shopName, extra) {
        return $.extend(true, {
            "configType": 1,
            "sceneFlag": "true",
            "sceneType": 2,
            "userName": userName,
            "shopName": shopName,
            "cfgTimeout": 60,
            "cloudModule": "xiaoxiaobeicfg",
            "deviceModule": "xiaoxiaobei",
            "nasId": shopId
        }, extra);
    }

    /**
     * 获取ssid的配置
     * @param userName
     * @param shopName
     * @param shopId
     * @param callback
     *
     * @return {Object}
     */
    v3AntConfmgr.getSsisCfg = function (userName, shopName, shopId, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "method": "GetSsisCfg",
            "param": [{
                "userName": userName,
                "sceneName": shopName,
                "nasId": shopId
            }]
        }), 10000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            var result = data.result || [];
            var manList = [],
                visitList = [];
            result.forEach(function (p1) {
                if (p1.spName === 'sp_manage_00') {
                    visitList.push(p1);
                } else {
                    manList.push(p1);
                }
            });
            callback(0, {
                manList: manList,
                visitList: visitList
            });
        });
    };

    /**
     * 恢复配置
     * @param userName
     * @param shopId
     * @param shopName
     * @param callback
     */
    v3AntConfmgr.restoreConfig = function (userName, shopId, shopName, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "method": "RestoreCurcfg",
            "param": [{
                "nasId": shopId,
                "userName": userName,
                "sceneName": shopName
            }]
        }), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.result === 'success') {
                return callback(0);
            }
            return callback(1, data.reason);
        });
    };

    /**
     * 恢复配置
     * @param userName
     * @param shopId
     * @param shopName
     * @param callback
     */
    v3AntConfmgr.backupConfig = function (userName, shopId, shopName, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "method": "BackupAllCfg",
            "param": [{
                "nasId": shopId,
                "userName": userName,
                "sceneName": shopName
            }]
        }), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.result === 'success') {
                return callback(0);
            }
            return callback(1, data.reason);
        });
    };

    /**
     * 更新开关
     * @param userName
     * @param shopId
     * @param shopName
     * @param ssidInfo
     * @param callback
     */
    v3AntConfmgr.ssidUpdate = function (userName, shopId, shopName, ssidInfo, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "configType": 0,
            "method": "SSIDUpdate",
            "param": [{
                "nasId": shopId,
                "userName": userName,
                "sceneName": shopName,
                "spName": ssidInfo.spName,
                "ssidName": ssidInfo.ssidName,
                "status": ssidInfo.status,
                "hideSSID": ssidInfo.hideSSID,
                "maxClients": ssidInfo.maxClients,
                "securityMode": ssidInfo.securityMode,
                "psk": ssidInfo.psk,
                "upSpeedLimit": ssidInfo.upSpeedLimit,
                "downSpeedLimit": ssidInfo.downSpeedLimit,
                "bDefaultSsid": ssidInfo.bDefaultSsid,
                "serviceType": ssidInfo.serviceType,
                "portalType": ssidInfo.portalType
            }]
        }), 60000, function (code, data) {
            if (code === 1 || code === 2) {
                return callback(2);
            }
            if (data.serviceResult === 'success') {
                //  TODO 判断设备是否成功,如果有一个失败的就显示失败
                var deviceSucc = true;
                data.deviceResults.forEach(function (p1) {
                    if (p1.reason == 'Failed to set cfg to device.') {
                        deviceSucc = false;
                    }
                });
                if (!deviceSucc) {
                    return callback(1, locale.base.err.devConn);
                }
                return callback(0);
            }
            return callback(1, data.reason);
        });
    };

    /**
     * 获取信号强度
     * @param userName
     * @param shopId
     * @param shopName
     * @param devSN
     * @param callback
     */
    v3AntConfmgr.getPowerLevel = function (userName, shopId, shopName, devSN, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "devSN": devSN,
            "cfgTimeout": 10,
            "method": "GetPowerLevel",
            "param": [{
                "nasId": shopId,
                "userName": userName,
                "sceneName": shopName,
                "type": "2"
            }]
        }), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.reason === '') {
                return callback(0, data.result);
            }
            return callback(1, data.reason);
        });
    };

    /**
     * 设置级别
     * @param userName
     * @param shopId
     * @param shopName
     * @param info {radio,powerLevel,devSN}
     * @param callback
     */
    v3AntConfmgr.setPowerLevel = function (userName, shopId, shopName, info, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "devSN": info.devSN,
            "method": "PowerLevel",
            "policy": "cloudFirst",
            "configType": 0,
            "cfgTimeout": 120,
            "param": [{
                "userName": userName,
                "sceneName": shopName,
                "radio": info.radio,
                "nasId": shopId,
                "powerLevel": info.powerLevel
            }]
        }), 60000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.serviceResult === 'success') {
                //  TODO 判断设备是否成功,如果有一个失败的就显示失败
                var deviceSucc = true;
//              data.deviceResults.forEach(function (p1) {
//                  if (p1.reason == 'Failed to set cfg to device.') {
//                      deviceSucc = false;
//                  }
//              });
                if (!deviceSucc) {
                    return callback(1, locale.base.err.devConn);
                }
                return callback(0);
            }
            return callback(1, data.reason);
        });
    };

    /**
     * 获取wifi开关时间
     * @param {Object} userName
     * @param {Object} shopId
     * @param {Object} shopName
     * @param {Object} devSN
     * @param {Object} callback
     */
    v3AntConfmgr.getWifiTimer = function (userName, shopId, shopName, devSN, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "devSN": devSN,
            "cfgTimeout": 120,
            "method": "GetWiFiTimer",
            "param": [{
                "userName": userName,
                "nasId": shopId,
                "sceneName": shopName
            }]
        }), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.reason) {
                return callback(1, data.reason);
            }
            callback(0, data.result);
        });
    };

    /**
     * 更新wifi定时开关
     * @param userName
     * @param shopId
     * @param shopName
     * @param info {devSN,start,stop,enable,period}
     * @param callback
     */
    v3AntConfmgr.wifiTimerUpdate = function (userName, shopId, shopName, info, callback) {
        var start = info.start.split(':');
        var stop = info.stop.split(':');
        if (start.length < 2 || stop.length < 2) {
            return;
        }
        var starth = Number(start[0]),
            startm = Number(start[1]),
            stoph = Number(stop[0]),
            stopm = Number(stop[1]);
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "devSN": info.devSN,
            "method": "WiFiTimerUpdate",
            "policy": "cloudFirst",
            "configType": 0,
            "cfgTimeout": 120,
            "param": [{
                "userName": userName,
                "sceneName": shopName,
                "enable": info.enable,
                "starth": starth,
                "startm": startm,
                "stoph": stoph,
                "stopm": stopm,
                "nasId": shopId,
                "period": info.period
            }]
        }), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.serviceResult === 'success') {
                //  TODO 判断设备是否成功,如果有一个失败的就显示失败
                var deviceSucc = true;
//              data.deviceResults.forEach(function (p1) {
//                  if (p1.reason == 'Failed to set cfg to device.') {
//                      deviceSucc = false;
//                  }
//              });
                if (!deviceSucc) {
                    return callback(1, locale.base.err.devConn);
                }
                return callback(0);
            }
            return callback(1, data.reason);
        });
    };

    /**
     * 获取qos配置
     * @param userName
     * @param shopId
     * @param shopName
     * @param callback
     */
    v3AntConfmgr.getQosConf = function (userName, shopId, shopName, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "cfgTimeout": 10,
            "method": "GetScenariosCfg",
            "param": [{
                "nasId": shopId,
                "userName": userName,
                "sceneName": shopName,
                "type": "2"
            }]
        }), function (code, data) {
            if (code === 1 || code === 2) {
                return callback(2);
            }
            if (!data.result) {
                return callback(1, locale.getErrMsg); //  获取ssid设备失败
            }
            return callback(0, data.result);
        });
    };

    /**
     * 更新qos配置
     * @param userName
     * @param shopId
     * @param shopName
     * @param info
     * @param callback
     */
    v3AntConfmgr.updateQosConf = function (userName, shopId, shopName, info, callback) {
        baseRequest.commonPost(baseUrl, execParams(userName, shopId, shopName, {
            "configType": 0,
            "method": "QoSCfgUpdate",
            "policy": "cloudFirst",
            "param": [
                {
                    "nasId": shopId,
                    "userName": userName,
                    "sceneName": shopName,
                    "type": "2",
                    "qosEnable": info.qosEnable,
                    "downBandwidth": info.downBandwidth,
                    "guestRatio": (100 - info.guestRatio) / 100,
                    "guestMode": 0
                }
            ]
        }), function (code, data) {
        	console.log(JSON.stringify(data));
            if (code === 1 || code === 2) {
                return callback(2);
            }
            if (data.serviceResult === 'success') {
                //  TODO 判断设备是否成功,如果有一个失败的就显示失败
                var deviceSucc = true;
//              data.deviceResults.forEach(function (p1) {
//                  if (p1.reason == 'Failed to set cfg to device.') {
//                      deviceSucc = false;
//                  }
//              });
                if (!deviceSucc) {
                    return callback(1, locale.base.err.devConn);
                }
                return callback(0);
            }
            return callback(1, data.reason);
        });
    }
//add by dongyi
	v3AntConfmgr.deleteAuthTerm = function(devSN,userMacAddr,callback){
		baseRequest.commonPost(baseUrl, {
			"cfgTimeout": 5,
			"devSN": devSN,
            "configType": 0,
            "cloudModule": "portal",
            "method": "DeleteUser",
            "policy": "cloudFirst",
            "deviceModule": "portal",
            "param": [
                {
                    "branchName ": "",
                    "userMacAddr": userMacAddr
                }
            ]
        }, 10000, function (code, data) {
            if (code === 1 || code === 2) {
                return callback(2);
            }
            if(code === 0) {
				if(data.errCode === 0) {
					callback(0, data);
				} else {
					callback(1);
				}
			}
        });
	}
	
	//添加无线服务
	v3AntConfmgr.addWireless = function(devSN,param,callback){
		baseRequest.commonPost(baseUrl,{
			"devSN":devSN,
			"configType":0,
			"module":"stamgr",
			"cloudModule":"stamgr",
			"deviceModule":"stamgr",
			"method":"SSIDUpdate",
			"param":[param]
		}, 1000000, function(code, data){
			if (code === 1 || code === 2) {
                return callback(2);
            }
            if(code === 0) {
				if(data.result === "success") {
					callback(0);
				} else {
					callback(1,data.reason);
				}
			}
		})
	}
	
	//同步SSID
	v3AntConfmgr.synchroSSID = function(devSN,param,callback){
		baseRequest.commonPost(baseUrl,{
			"devSN":devSN,
			"configType":0,
			"module":"stamgr",
			"cloudModule":"stamgr",
			"deviceModule":"stamgr",
			"method":"SyncSSIDList",
			"param":[param]
		}, 1000000, function(code, data){
			if (code === 1 || code === 2) {
                return callback(2);
            }
            if(code === 0) {
				if(data.result === "success") {
					callback(0);
				} else {
					callback(1,data.reason);
				}
			}
		})
	}
	
//end

	//add by wangyu
	v3AntConfmgr.getProbe = function(devSN, skip, method, callback){
		baseRequest.commonPost(baseUrl, {
			devSN:devSN,
			cloudModule: 'WIPS',
			configType: 1,
			method: method,
			param:{
				skip: skip,
				limit: 10,
				sort: 1
			}
		}, 10000, function(code, data){
			if (code === 1 || code === 2) {
                return callback(2);
            }
            if(code === 0) {
				if(data.reason.result === "success") {
					callback(0, data.result);
				} else {
					callback(1);
				}
			}
		})
	}
	
	v3AntConfmgr.getTurnOnOff = function(devSN, param, callback){
		baseRequest.commonPost(baseUrl, {
			devSN:devSN,
			cloudModule: 'PROBE',
			configType: 0,
			deviceModule: "PROBE",
			method: "probeEnable",
			param: param
		}, 10000, function(code, data){
			 if (code === 1 || code === 2) {
                return callback(2);
            }
            if(code === 0) {
				if(data.errCode === 0) {
					callback(0, data);
				} else {
					callback(1);
				}
			}
		})
	}
	
	
	//end

})(mui, window);